package com.wnxy.sdd.service.impl;
import java.util.Date;
import java.util.List;

import com.wnxy.sdd.entity.Course;
import com.wnxy.sdd.entity.CourseDescription;
import com.wnxy.sdd.mapper.CourseDescriptionMapper;
import com.wnxy.sdd.mapper.CourseMapper;
import com.wnxy.sdd.model.vo.CoursePublishVo;
import com.wnxy.sdd.model.vo.CourseVo;
import com.wnxy.sdd.service.ICourseDescriptionService;
import com.wnxy.sdd.service.ICourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author Jet
 * @since 2023-06-19
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements ICourseService {

    private final ICourseDescriptionService courseDescriptionService;

    public CourseServiceImpl(ICourseDescriptionService courseDescriptionService) {
        this.courseDescriptionService = courseDescriptionService;
    }

    // 事务注解
    @Transactional
    @Override
    public String saveOrUpdateCourse(CourseVo courseVo) {
        // 保存课程：sdd_course
        Course course = new Course();
        // 对象拷贝; 参数1：数据源； 参数2：要拷贝的目标对象
        BeanUtils.copyProperties(courseVo,course);
        // 保存课程，自动把课程id回填到course对象中。
        this.saveOrUpdate(course);

        // 保存课程描述：sdd_course_description
        CourseDescription cd = new CourseDescription();
        cd.setId(course.getId()); // 公用同一个主键；且设置主键手动指定
        cd.setDescription(courseVo.getDescription());
        courseDescriptionService.saveOrUpdate(cd);
        // 返回课程ID
        return course.getId();
    }

    @Override
    public CourseVo findCourseById(String id) {
        // 课程信息
        Course course = this.getById(id);
        CourseVo courseVo = new CourseVo();
        BeanUtils.copyProperties(course,courseVo);
        // 课程详情
        CourseDescription cd = courseDescriptionService.getById(id);
        courseVo.setDescription(cd.getDescription());
        return courseVo;
    }

    @Override
    public CoursePublishVo findCoursePublish(String courseId) {
        return baseMapper.findCoursePublish(courseId);
    }
}
